package Demo.demo23;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Scanner;

public class BookManager {
    Book[] books = new Book[50];
    Scanner input = new Scanner(System.in);

    public BookManager() {
        books[0] = new Book(1, "斗破苍穹", 1, 100, null);
        books[1] = new Book(2, "斗罗大陆", 0, 90, "2022-01-11");
        books[2] = new Book(3, "保淞爱情故事", 0, 99, "2022-02-23");
        books[3] = new Book(4, "霸道总裁", 1, 80, null);
        books[4] = new Book(5, "都市公子", 0, 81, "2022-04-01");
    }

    public void menu() {
        String answer = "y";
        //循环判断是否继续
        while (answer.equalsIgnoreCase("y")) {
            System.out.println("欢迎使用图书管理系统");
            System.out.println("--------------------");
            System.out.println("1.查看图书");
            System.out.println("2.新增图书");
            System.out.println("3.删除图书");
            System.out.println("4.借出图书");
            System.out.println("5.归还图书");
            System.out.println("6.排行榜");
            System.out.println("0.返回主菜单");
            System.out.println("--------------------");
            System.out.println("请选择0-6:");
            int choose = input.nextInt();
            switch (choose) {
                case 1:
                    list();
                    break;
                case 2:
                    add();
                    break;
                case 3:
                    delete();
                    break;
                case 4:
                    lend();
                    break;
                case 5:
                    returnBook();
                    break;
                case 6:
                    rank();
            }


        }
    }

    public void rank() {
        String [] names = new String[50];
        int [] counts = new int[50];

        for (int i = 0; i < books.length; i++) {
            if(books[i] != null){
                names[i] = books[i].name;
                counts[i] = books[i].count;
            }
        }
        int temp = 0;
        String tempName = "";
        for (int i = 0; i < counts.length -1; i++) {
            for (int j = 0; j < counts.length - i - 1; j++) {
                if(counts[j] < counts[j+1]){
                    temp = counts[j+1];
                    tempName = names[j+1];
                    counts[j+1] = counts[j];
                    names[j+1] =  names[j];
                    counts[j] = temp;
                    names[j] = tempName;
                }
            }
        }

        System.out.println("图书排行榜");
        System.out.println("图书名称    借出次数");
        for (int i = 0; i < names.length; i++) {
            if(names[i] != null){
                System.out.println(names[i] + "   " + counts[i]);
            }
        }
    }


    public void returnBook(){
        System.out.println("请输入你要归还图书的名字:");
        String returnName = input.next();

        for (int i=0; i<books.length; i++){
            boolean flag  = true;
            //如果书籍的位置不为空
            if(books[i] != null){
                if(books[i].name.equals(returnName) && books[i].status==0){
                    System.out.println("请输入归还的时间:");
                    String returnTime = input.next();

                    long loan = getLoan(books[i].time,returnTime);

                    System.out.println("归还成功:");
                    System.out.println("租金：" + loan);
                    books[i].status = 1;
                    books[i].time = null;
                    break;
                }
                else if(books[i].name.equals(returnName) && books[i].status==1){
                    System.out.println("该图书没有被借出，你是不是傻~");
                    break;
                }
            }
        }
    }

    public long getLoan(String startTime,String endTime){
       boolean flag  = true;
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        long diff = 0;
        try {
            Date d1 = sdf.parse(startTime);
            Date d2 = sdf.parse(endTime);
            if (d2.getTime()>d1.getTime()){
                diff = (d2.getTime() / (1000*60*60*24)) - (d1.getTime() / (1000*60*60*24));
                flag = true;
            }
        } catch (ParseException e) {
            e.printStackTrace();
        }

        return diff;
    }


    //借出图书
    public void lend(){
      //定义一个下标
        int index = -1;

        System.out.println("请输入你要借出图书的名字:");

        String lendName = input.next();
//给出一个判断值 方便后续输出
        int flag = 0;

        for (int i=0; i<books.length; i++){
           //如果书的位置不为空
            if(books[i] != null){
                //如果有这本书 并且这本书未借出
                if(books[i].name.equals(lendName) && books[i].status == 1){
                    //下标等于i的位置
                    index = i;
                   //更改判断值
                    flag = 1;
                    break;
                }
              //有这本书 但是这本书被借走了
                else if(books[i].name.equals(lendName) && books[i].status == 0){
                    flag = -1;
                }
            }
        }
        //当flag=1的时候
        if(flag == 1){
           //借出变为0 代表已经被借走了
            books[index].status = 0;
           //借出次数++
            books[index].count ++;
            System.out.println("请输入你要借出的时间:");
            //输入借出时间
            books[index].time = input.next();

            System.out.println("借出成功！");
        }
        else if(flag == -1){
            System.out.println("找到你要借出的图书，但是该图书已经被借出，不能重复借出");
        }
        else{
            System.out.println("没有找到您要借出的图书");
        }
    }

    public void delete() {
        //输入要删除图书的名字
        System.out.println("请输入你要删除图书的名字:");
      //输入一个字符串
        String delName = input.next();

        //设置一个标志，表示是否删除成功
        boolean flag = false;

        //找到要删除的图书然后从数组中删除
        for (int i=0; i<books.length; i++){
            //在应该有的数据中查找，！= null
            if(books[i] != null){
                //要删除的图书名字一样并且不能是被借出状态
                if(books[i].name.equals(delName) && books[i].status == 1){
                  //把数组i的位置赋值给j
                    int j = i;
                  //while循环 把后一本书的值赋值给前一位
                    while(books[j+1] != null){
                        books[j].id = books[j+1].id;
                        books[j].name = books[j+1].name;
                        books[j].status = books[j+1].status;
                        books[j].count = books[j+1].count;
                        books[j].time = books[j+1].time;
                        j++;
                    }
                    //最后把j赋值给空
                    books[j] = null;
                    //给出flag判断 方便后续输出
                    flag = true;
                }
                //如果有这本书 但是这本书本借出了  就会改变flag的值
                else if(books[i].name.equals(delName) && books[i].status == 0){
                    flag = false;
                }
            }
        }

        if(flag){
            System.out.println("删除成功！");
        }
        else{
            System.out.println("删除失败，没有你要删除的图书或者该图书是借出状态");
        }

    }

    //添加图书
    private void add() {
        int id = -1;
        for (int i = 0; i < books.length; i++) {
            if (books[i] == null) {
                id = i;
                break;
            }
        }
        if (id != -1) {
            books[id] = new Book();
            books[id].id = id + 1;
            System.out.println("请输入新图书的名字");
            books[id].name = input.next();
            books[id].status = 1;
            books[id].count = 0;
            books[id].time = null;
            System.out.println("新图书添加成功");
        } else {
            System.out.println("图书已满");
        }
    }


    //查看图书
    private void list() {
        for (int i = 0; i < books.length; i++) {
            if (books[i] != null) {
                System.out.println(books[i].name);
            } else {
                break;
            }
        }
    }
}
